/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;

/**
 *
 * This bean handles validation of fileds and return corresponding error strings.
 * 
 * @author jonas
 */
public class ValidatorBean {

    public void validateField(FacesContext context, UIComponent validate, Object value){

        String field = (String)value;
        boolean valid = true;
        String msgStr = "";

        if(field.length() == 0){
            msgStr = "Field must not be empty";
            valid = false;
        }
        else if(field.length() < 2){
            msgStr = "Field value must be longer than 2";
            valid = false;
        }
        else if(field.length() > 30){
            msgStr = "Field value must not be longer than 30";
            valid = false;
        }

        if(!valid){
            ((UIInput)validate).setValid(false);
            FacesMessage msg = new FacesMessage(msgStr);
            context.addMessage(validate.getClientId(context), msg);
        }
    }
}
